<template>
  <div>
    <div style="margin-bottom: 10px; font-size: 14px; font-weight: 250; margin-left: 10px">
      <el-icon><DataAnalysis /></el-icon>
      学院管理
    </div>
    <el-card style>
      <div style="display: flex">
        <el-button type="primary" size="large" @click="addDept">创建学院</el-button>
        <el-input placeholder="请输入学院名称" v-model="data.deptName" style="width: 300px; margin-left: 940px; margin-right: 20px"  prefix-icon="Search"/>
        <el-button type="primary" size="large" @click="load">搜索</el-button>
      </div>
      <el-table :data="data.tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="学院ID" />
        <el-table-column prop="name" label="学院名" />
        <el-table-column prop="majorCount" label="专业数" />
        <el-table-column prop="teacherCount" label="教师数" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="updateDept(scope.row)" type="primary" icon="Edit" circle />
            <el-button @click="handleDel(scope.row.id)" type="danger" icon="Delete" circle />
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: center; margin-top: 10px">
        <el-pagination layout="prev, pager, next" :total="data.tableData.length" :page-size="10" />
      </div>
    </el-card>
    <el-dialog v-model="data.dialogFormVisible" title="编辑" width="500">
      <el-form ref="formRef" :rules="data.rules" :model="data.form" label-width="80px" style="padding: 20px">
        <el-form-item label="学院名称" prop="name">
          <el-input size="large" v-model="data.form.name" placeholder="请输入学院名称"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="data.dialogFormVisible = false" size="large">取消</el-button>
          <el-button type="primary" @click="handleSave" size="large">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>

import {reactive, ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage, ElMessageBox} from "element-plus";

const formRef = ref()

const data = reactive({
  tableData: [],
  deptName: null,
  dialogFormVisible: false,
  form: {},
  rules: {
    name: [
      {required: true, message: "请输入学院名称!", trigger: "blur"}
    ],
  },
  isAdd: true,
})

const load = () => {
  request.get("admin/selectAllDept", {params: {name: data.deptName}}).then(res => {
    data.tableData = res.data
  })
}

load()

const addDept = () => {
  data.dialogFormVisible = true;
  data.form = {
    name: ""
  };
  data.isAdd = true
}

const handleAdd = () => {
  request.post("admin/addDept", data.form).then(res => {
    if (res.code === "200") {
      ElMessage.success("创建成功")
      load()
      data.dialogFormVisible = false
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const updateDept = (row) => {
  data.form = JSON.parse(JSON.stringify(row))  // 深拷贝
  data.dialogFormVisible = true
  data.isAdd = false
}

const handleEdit = () => {
  request.put("admin/updateDept", data.form).then(res => {
    if (res.code === "200") {
      ElMessage.success("更新成功")
      load()
      data.dialogFormVisible = false
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const handleSave = () => {
  formRef.value.validate((valid) => {
    if(valid) {
      data.isAdd ? handleAdd() : handleEdit()
    }
  })
}

const handleDel = (id) => {
  ElMessageBox.confirm(
      '删除后无法复原，是否确定删除',
      '删除提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    request.delete("admin/deleteDept/" + id).then(res => {
      if(res.code === "200") {
        ElMessage.success("删除成功")
        load()
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消删除',
    })
  })
}
</script>